package com.dq.service.finance.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.RewardQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.domain.base.impl.BaseDaoImpl;

import com.dq.domain.org.Org;
import com.dq.domain.reward.Reward;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.finance.RewardService;
import com.dq.service.org.OrgService;
import com.dq.utils.BeanNullUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Map;

/**
 * @author liuhui
 * @date 2019/11/28 0028 15:18:29
 */
@Service
public class RewardServiceImpl extends BaseRepositories implements RewardService {
    @Autowired
    private OrgService orgService;


    @Override
    public List<Reward> findByWhere(RewardQuery rewardQuery, Org org) throws AdminException {
        rewardQuery.format();
        Map<String, Object> query = rewardQuery.parseMap();
        if(rewardQuery.getOrgId() != null){
            query.put("org_id",rewardQuery.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("org" + BaseDaoImpl.inSqlStr, orgList);
        }
        if(rewardQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,rewardQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(rewardQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,rewardQuery.getEndTime());
            query.remove("endTime");
        }
        if(StringUtils.isNotEmpty(rewardQuery.getCardNo())){
            query.put("staffUser_cardNo"+BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(rewardQuery.getCardNo()));
            query.remove("cardNo");
        }
        if(StringUtils.isNotEmpty(rewardQuery.getUserName())){
            query.put("staffUser_userRealcName"+BaseDaoImpl.likeSqlStr,BaseAdminQuery.likeSql(rewardQuery.getUserName()));
            query.remove("userName");
        }
        if(StringUtils.isNotEmpty(rewardQuery.getCategory().toString())){
            query.put("rewardCategory",rewardQuery.getCategory());
            query.remove("category");
        }
        List<Reward> rewards = dao.find(dao.query2Hql(Reward.class, query), query);
        return rewards;
    }

    @Override
    public Long countByWhere(RewardQuery rewardQuery, Org org) throws AdminException {
        rewardQuery.format();
        Map<String, Object> query = rewardQuery.parseMap();
        if(rewardQuery.getOrgId() != null){
            query.put("org_id",rewardQuery.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("org" + BaseDaoImpl.inSqlStr, orgList);
        }
        if(rewardQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,rewardQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(rewardQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,rewardQuery.getEndTime());
            query.remove("endTime");
        }
        if(StringUtils.isNotEmpty(rewardQuery.getCardNo())){
            query.put("staffUser_cardNo"+BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(rewardQuery.getCardNo()));
            query.remove("cardNo");
        }
        if(StringUtils.isNotEmpty(rewardQuery.getUserName())){
            query.put("staffUser_userRealcName"+BaseDaoImpl.likeSqlStr,BaseAdminQuery.likeSql(rewardQuery.getUserName()));
            query.remove("userName");
        }
        if(StringUtils.isNotEmpty(rewardQuery.getCategory().toString())){
            query.put("rewardCategory",rewardQuery.getCategory());
            query.remove("category");
        }
        Long count = dao.count(dao.count2Hql(Reward.class,query),query);
        return count;
    }

    @Override
    public Vo add(Long orgId, Long staffUserId, Reward reward) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        SysSection org = dao.get(SysSection.class,orgId);
        if(org == null){
            throw new AdminException("此门店不存在");
        }
        StaffUser staffUser = dao.get(StaffUser.class,staffUserId);
        if(staffUser == null){
            throw new AdminException("此员工不存在");
        }
        reward.setOrg(org);
        reward.setStaffUser(staffUser);
        reward.setVersion(Constant.VERSION_VALUE);
        reward.setDeleted(false);
        reward.setCreateTime(System.currentTimeMillis());
        reward.setLastModifyTime(System.currentTimeMillis());
        reward.setCreateUser(sysUser);
        reward.setUpdateUser(sysUser);
        rewardRepository.save(reward);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo edit(Long orgId, Long staffUserId, Reward rewardEdit) throws AdminException {
        Reward reward = dao.get(Reward.class,rewardEdit.getId());
        if(reward == null){
            throw new AdminException("请重新选择");
        }
        SysUser sysUser = shiroService.getUser();
        SysSection org = dao.get(SysSection.class,orgId);
        if(org == null){
            throw new AdminException("此门店不存在");
        }
        StaffUser staffUser = dao.get(StaffUser.class,staffUserId);
        if(staffUser == null){
            throw new AdminException("此员工不存在");
        }
        rewardEdit.setOrg(org);
        rewardEdit.setStaffUser(staffUser);
        rewardEdit.setUpdateUser(sysUser);
        rewardEdit.setLastModifyTime(System.currentTimeMillis());
        BeanUtils.copyProperties(rewardEdit,reward, BeanNullUtil.getNullPropertyNames(rewardEdit));
        rewardRepository.save(reward);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo delete(Long id) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        Reward reward = dao.get(Reward.class,id);
        reward.setUpdateUser(sysUser);
        reward.setDeleted(true);
        reward.setLastModifyTime(System.currentTimeMillis());
        rewardRepository.saveAndFlush(reward);
        return new Vo(HttpStatus.OK);
    }

}
